import axios from "axios";

class Request {
    /**
     * @param config {Object}   配置
     * @param reqSuccessFn {function}  请求成功回调
     * @param reqFailFn {function}  请求失败回调
     * @param respSuccessFn {function}  响应成功回调
     * @param respFailFn {function} 响应失败回调
     * */
    constructor(config, reqSuccessFn, reqFailFn, respSuccessFn, respFailFn) {
        this.instance = axios.create(config)
        this.instance.interceptors.request.use(reqSuccessFn, reqFailFn)
        this.instance.interceptors.response.use(respSuccessFn, respFailFn)
    }

    /**
     * @param config {Object}
     * @example
     * */
    request(config) {
        return this.instance.request(config)
    }

    /**
     * @param config {Object}
     * @example get({
     *     url: '/path'
     * })
     * */
    get(config) {
        return this.request({...config, method: 'get'})
    }

    /**
     * @param config {Object}
     * @example post({
     *     url: '/path'
     * })
     * */
    post(config) {
        return this.request({...config, method: 'post'})
    }

    /**
     * @param config {Object}
     * */
    put(config) {
        return this.request({...config, method: 'put'})
    }
}

export default Request